﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;

namespace CuaHangBangDia.QLHD
{
    public partial class frmThemHoaDon : Form
    {
        double g = 0;

        private bool _selectAll = false;
        public frmThemHoaDon()
        {
            InitializeComponent();
        }

        private void btnThemHoaDon_Click(object sender, EventArgs e)
        {
            int ma = 0;
            int mabd = 0;
            ChiTietHoaDon ct = new ChiTietHoaDon();
            if (dgvThemHoaDon.Rows.Count > 0)
            {
                for (int i = 0; i < dgvThemHoaDon.Rows.Count; i++)
                {
                    string ne = dgvThemHoaDon.Rows[i].Cells[0].Value.ToString();
                    HoaDon bd = GetDataHD(i);
                    HoaDon.Objects.Add(bd);
                    DateTime t = bd.NgayLap;
                    List<BangDia> b = BangDia.Objects.Find(q => q.TenBangDia == ne).ToList();
                    //List<BangDia> d=BangDia.Objects.Find(d=>d.)
                    foreach (BangDia a in b)
                    {
                        mabd = a.MaBangDia;
                    }

                    List<HoaDon> li = HoaDon.Objects.Find(d => d.NgayLap == t).ToList();
                    foreach (HoaDon item in li)
                    {
                        ma = item.MaHoaDon;
                    }
                    ct.MaHoaDon = ma;
                    ct.MaBangDia = mabd;
                    ct.SoLuongBan = Convert.ToInt32(dgvThemHoaDon.Rows[i].Cells[2].Value);
                    ChiTietHoaDon.Objects.Add(ct);
                }
            }

        }


        private void dgvThemHoaDon_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int n = dgvThemHoaDon.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvThemHoaDon.Rows[idx].Selected = _selectAll;
                    dgvThemHoaDon.Rows[idx].Cells[0].Value = _selectAll;
                }
                

            }
        }
        private HoaDon GetDataHD(int i)
        {
            HoaDon hd = new HoaDon();
            hd.NgayLap = DateTime.Now;
            hd.NhanVienLap = "Huu";
            hd.ThanhTien = Convert.ToDouble(lblTongTien.Text);
            return hd;
        }
        private void frmThemHoaDon_Load(object sender, EventArgs e)
        {
           


        }

        private void dgvThemHoaDon_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            if (dgvThemHoaDon.CurrentCell.ColumnIndex == 0 && e.Control is TextBox)
            {

                txt.Multiline = false;//This is important.
                txt.AutoCompleteCustomSource = new AutoCompleteStringCollection();
                List<BangDia> li = BangDia.Objects.GetAll().ToList();
                foreach (BangDia b in li)
                {
                    txt.AutoCompleteCustomSource.Add(b.TenBangDia);

                }
                txt.AutoCompleteMode = AutoCompleteMode.Suggest;
                txt.AutoCompleteSource = AutoCompleteSource.CustomSource;

            }
            else if (dgvThemHoaDon.CurrentCell.ColumnIndex != 0 && txt != null)
            {
                txt.AutoCompleteMode = AutoCompleteMode.None;
            }
        }

        private void dgvThemHoaDon_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int dem = 0;
            
            for (int i = 0; i < dgvThemHoaDon.Rows.Count; i++)
            {
                if (dgvThemHoaDon.Rows[i].Cells[0].Value != null)
                {
                    double kq = LayGiaTheoTen(dgvThemHoaDon.Rows[i].Cells[0].Value.ToString());
                    dgvThemHoaDon.Rows[i].Cells[1].Value = kq.ToString();
                    int sl = (Convert.ToInt32(dgvThemHoaDon.Rows[i].Cells[1].Value) * Convert.ToInt32(dgvThemHoaDon.Rows[i].Cells[2].Value));
                    dgvThemHoaDon.Rows[i].Cells[3].Value = sl;
                    dem += Convert.ToInt32(dgvThemHoaDon.Rows[i].Cells[3].Value.ToString());
                }
                lblTongTien.Text = dem.ToString();
            }
        }

        private double LayGiaTheoTen(string ten)
        {
            double g = 0;
            for (int i = 0; i < dgvThemHoaDon.Rows.Count; i++)
            {
     
                List<BangDia> p = BangDia.Objects.Find(q => q.TenBangDia == ten).ToList();
                foreach (BangDia a in p)
                {
                    g = a.Gia;
                }
            } return g;
        }

        private void TinhTongTien()
        {
            int dem = 0;
            for (int i = 0; i < dgvThemHoaDon.Columns.Count; i++)
            {
                if (dgvThemHoaDon.Rows[i].Cells[2].Value!=null)
                    dem += Convert.ToInt32(dgvThemHoaDon.Rows[i].Cells[3].Value.ToString());
                


            }
            lblTongTien.Text = dem.ToString()+"VNĐ";
        }
       



    }
}
